package wallet

import (
	"encoding/json"

	"gitee.com/xgotool/backend-framework-template/config"
	"gitee.com/xgotool/backend-framework-template/model"
	"gitee.com/xgotool/backend-framework-template/service/users"
	"gitee.com/xiaoyutab/xgotool/individual/xginplus"
	"gitee.com/xiaoyutab/xgotool/xstring"
)

// 后台钱包列表接口
//
//	GET	admin/user/wallet/list
func List(c *xginplus.Context) *xginplus.Response {
	type temp_struct struct {
		UserName  string      `json:"user_name" form:"user_name" xml:"user_name"`    // 用户名
		StartTime json.Number `json:"start_time" form:"start_time" xml:"start_time"` // 最后一次交易时间-开始(unix时间戳)
		EndTime   json.Number `json:"end_time" form:"end_time" xml:"end_time"`       // 最后一次交易时间-结束(unix时间戳)
	}
	temp := temp_struct{}
	if err := c.Bind(&temp); err != nil {
		return c.Error(err)
	}
	type temp_struct_used struct {
		UserName  string // 用户名
		StartTime int64  // 最后一次交易时间-开始(unix时间戳)
		EndTime   int64  // 最后一次交易时间-结束(unix时间戳)
	}
	param := temp_struct_used{}
	// 参数赋值
	param.UserName = temp.UserName
	param.StartTime = xstring.JInt64(temp.StartTime)
	param.EndTime = xstring.JInt64(temp.EndTime)
	// ApiPost软件所需要的参数描述格式:
	// [{"is_checked":1,"not_null":-1,"key":"user_name","type":"Text","field_type":"String","value":"","description":"用户名"},{"is_checked":1,"not_null":-1,"key":"start_time","type":"Text","field_type":"Integer","value":"","description":"最后一次交易时间-开始(unix时间戳)"},{"is_checked":1,"not_null":-1,"key":"end_time","type":"Text","field_type":"Integer","value":"","description":"最后一次交易时间-结束(unix时间戳)"},{"is_checked":1,"not_null":-1,"key":"page","type":"Text","field_type":"Integer","value":"","description":"当前页码"},{"is_checked":1,"not_null":-1,"key":"offset","type":"Text","field_type":"Integer","value":"","description":"跳过条数，传入此值后会忽略调 page 参数"},{"is_checked":1,"not_null":-1,"key":"limit","type":"Text","field_type":"Integer","value":"","description":"每页查询条数"}]
	// ApiFox软件所需要的参数描述格式:（此软件需要的参数为多行形式，请手动去除最前面的//和空格，再行复制）
	// "user_name","string","false","","","用户名"
	// "start_time","integer","false","","","最后一次交易时间-开始(unix时间戳)"
	// "end_time","integer","false","","","最后一次交易时间-结束(unix时间戳)"
	// "page","integer","false","","","当前页码"
	// "offset","integer","false","","","跳过条数，传入此值后会忽略调 page 参数"
	// "limit","integer","false","","","每页查询条数"
	// 其他软件所需要的KV键值对描述:
	// {"user_name":"用户名","start_time":"最后一次交易时间-开始(unix时间戳)","end_time":"最后一次交易时间-结束(unix时间戳)","page":"当前页码","offset":"跳过条数，传入此值后会忽略调 page 参数","limit":"每页查询条数"}
	// 本命令执行参数【留做备份，以便后期使用/修改】
	// user_name string 用户名 start_time int64 最后一次交易时间-开始(unix时间戳) end_time int64 最后一次交易时间-结束(unix时间戳) x.ginplus x.page
	// 参数效验部分

	conn, err := config.GetConnect()
	if err != nil {
		return c.Error(err)
	}
	mod, mods := model.Wallet{}, []model.Wallet{}
	var uid uint = 0
	var count int64
	if param.UserName != "" {
		uid, err = users.GetId(param.UserName)
		if err != nil {
			return c.Error(err)
		}
		if uid == 0 {
			return c.Errorf("用户不存在")
		}
	}
	err = conn.Table(mod.TableName()).
		Where("type", 0).When(uid > 0, "uid", uid).
		When(param.StartTime > 0, "updated_at >= ?", param.StartTime).
		When(param.EndTime > 0, "updated_at <= ?", param.EndTime).
		List(c.GetInt("offset"), c.GetInt("limit"), &count, &mods)
	if err != nil {
		return c.Error(err)
	}
	uids := []uint{}
	for _, mod := range mods {
		uids = append(uids, mod.Uid)
	}
	us, uss := model.User{}, []model.User{}
	err = conn.Table(us.TableName()).
		Select("id", "user_name", "nickname").
		Where("id IN ?", uids).
		Find(&uss).
		Error
	if err != nil {
		return c.Error(err)
	}
	for i, mod := range mods {
		for _, us := range uss {
			if mod.Uid == us.Id {
				mods[i].User = us
				break
			}
		}
	}
	return c.SuccessPage(count, mods)
}
